package org.jeecg.modules.dashboard.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.dashboard.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuganyue
 * @description 首页仪表盘控制器
 * @date 2025-09-19
 */
@Slf4j
@Api(tags = "首页仪表盘000")
@RestController
@RequestMapping("/procurement/dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取首页仪表盘所有统计数据
     * 支持传入时间参数（可选），默认为今天 00:00:00
     *
     * @param startTime 查询起始时间，格式：yyyy-MM-dd HH:mm:ss
     * @return 包含各项统计结果的 JSON 响应
     */
    @AutoLog(value = "首页仪表盘")
    @ApiOperation(value = "首页仪表盘", notes = "首页仪表盘")
    @GetMapping("/home/dashBorad")
    public Result<Object> getDashboardStatistics(
            @RequestParam(name = "startTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime) {

        if (startTime == null) {
            // 若输入为空，则按一个月前的日期获取
            LocalDate oneMonthAgo = LocalDate.now().minusMonths(1);
            startTime = Date.from(oneMonthAgo.atStartOfDay(ZoneId.systemDefault()).toInstant());
        } else {
            // 如果传入了 startTime，则按指定时间获取
            LocalDateTime localDateTime = LocalDateTime.ofInstant(startTime.toInstant(), ZoneId.systemDefault());
            LocalDate date = localDateTime.toLocalDate();
            startTime = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }

        Map<String, Object> data = new HashMap<>();
        try {
            // 1. 总供应商数量
            data.put("totalSuppliers", dashboardService.getTotalSuppliers());

            // 2. 进行中的项目数量
            data.put("ongoingProjects", dashboardService.getOngoingProjects());

            // 3. 今日开标的数量
            data.put("todayOpenTenders", dashboardService.getTodayOpenTenders(startTime));

            // 4. 活跃供应商数量（更新时间 >= startTime）
            data.put("activeSuppliers", dashboardService.getActiveSuppliers(startTime));

            // 5. 总计金额
            BigDecimal totalAmount = dashboardService.getTotalAmount();
            data.put("totalAmount", totalAmount != null ? totalAmount : BigDecimal.ZERO);

            return Result.OK(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取仪表盘数据失败：" + e.getMessage());
        }
    }

}
